package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private EditText editTextNumber4;
    private Button button;
    private TextView textView;
    private Handler handler;
    private Runnable runnable;
    private boolean isEnd = true;
    private String str;
    private int maxNum;
    private Random random;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextNumber4 = (EditText) findViewById(R.id.editTextNumber4);
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView);
        random = new Random();
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                    textView.setText(String.valueOf(random.nextInt(maxNum)));
                    handler.postDelayed(runnable, 50);
            }
        };
        button.setOnClickListener(v -> {
            str=editTextNumber4.getText().toString();
            if(!str.isEmpty()&&Integer.valueOf(str)>0) {
                maxNum=Integer.valueOf(str);
                if (isEnd) {
                    button.setText("暂停");
                    editTextNumber4.setInputType(InputType.TYPE_NULL);
                    handler.postDelayed(runnable, 0);
                } else {
                    button.setText("开始");
                    editTextNumber4.setInputType(InputType.TYPE_CLASS_TEXT);
                    handler.removeCallbacks(runnable);
                }
                isEnd=!isEnd;
            }
            else {
                new  AlertDialog.Builder(getCurrentActivity())
                        .setTitle("提示" )
                        .setMessage("请输入大于0的整数！" )
                        .setPositiveButton("确定" ,  null )
                        .show();
            }
        });
    }

    public static Activity getCurrentActivity () {  //获取当前的Activity对象
        try {
            Class activityThreadClass = Class.forName("android.app.ActivityThread");
            Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(
                    null);
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            activitiesField.setAccessible(true);
            Map activities = (Map) activitiesField.get(activityThread);
            for (Object activityRecord : activities.values()) {
                Class activityRecordClass = activityRecord.getClass();
                Field pausedField = activityRecordClass.getDeclaredField("paused");
                pausedField.setAccessible(true);
                if (!pausedField.getBoolean(activityRecord)) {
                    Field activityField = activityRecordClass.getDeclaredField("activity");
                    activityField.setAccessible(true);
                    Activity activity = (Activity) activityField.get(activityRecord);
                    return activity;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}